var ws = require("nodejs-websocket");
console.log("开始建立连接...")
var WebSocket = require('ws')
const moment = require('moment');
const pako = require('pako');
const axios = require('axios');

var game1 = null,
    game2 = null,
    game1Ready = false,
    game2Ready = false;

var orderbook = {};

exports.OrderBook = orderbook;

// WS_URL = 'wss://stream.binance.com:9443/ws/ethusdt@kline_1m';
var server = ws.createServer(function (conn) {
    conn.on("text", function (str) {
        // {"sub":"market.btcusdt.kline.1m"}
        let sub = JSON.parse(str)['sub'];
        let kline = sub.split('.')
        //console.log(chen[2]);

        // { "sub": "market', 'btcusdt', 'kline_1m" }
        WS_URL = 'wss://stream.binance.com:9443/ws/'+kline[1]+'@'+kline[2];
        console.log(WS_URL)
        var ws2 = new WebSocket(WS_URL);
        ws2.on('open', () => {
            console.log('open');
            // subscribe(ws);
        });
        ws2.on('message', (data) => {
            console.log(data);
            // console.log(JSON.parse(data).e);
            // conn.sendText((data));
        });

    })
    conn.on("close", function (code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function (code, reason) {
        console.log("异常关闭")
    });
}).listen(8008)
console.log("WebSocket建立完毕")
